<?php

/**
 * The APIServer and API Client Class allows two different websites to communicate site-to-site.
 * The APIServer class serves an encoded string that can be decoded by a client.
 * @author MATT
 */
class APIServer {

    private $assocArray;
    private $encodeCommand;

    public function __construct($array, $enctype = 'json'){

        $this->assocArray = $array;
        $this->encodeCommand = ($enctype == 'json') ? 'json_encode' : 'serialize';
    }

    public function serve(){
        return call_user_func($this->encodeCommand, $this->assocArray);
    }

}

class APIClient {

    private $url;
    private $decodeCommand;

    public function __construct($url, $enctype = 'json'){

        $this->url = $url;
        $this->decodeCommand = ($enctype == 'json') ? 'json_decode' : 'unserialize';

    }

    public function get(){
        $ch = curl_init($this->url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $encoded = curl_exec($ch);

        return call_user_func($this->decodeCommand, $encoded);
    }
}
?>
